
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>

#include "bocat/common/zlib.hpp"

using namespace bocat::common;


int main()
{
	const char*	plains = "hello,world.--AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

	{
		char*	zbuf = NULL;
		size_t	zsize = 0;

		ZlibRawCompressor	ZLIBRAWCompressor;
		Compressor*	compressor_p = &ZLIBRAWCompressor;
		fprintf( stdout, "compressor_p = %p\n", compressor_p );
		zbuf = compressor_p->compress( plains, strlen(plains), &zsize );
		if ( NULL == zbuf ) {
			std::cerr << "fail to compress in RAW mode\n";
			return	1;
		}
		std::cout << "RAW compress  size : " << strlen(plains) << " --> " << zsize << std::endl;

		char*	buf = NULL;
		size_t	size = 0;
		buf = compressor_p->decompress( zbuf, zsize, &size );
		if ( NULL == buf ) {
			std::cerr << "fail to decompress in RAW mode\n";
			return	1;
		}
		std::cout << "RAW decompress  size : " << zsize << " --> " << size << std::endl;
		std::cout << "RAW decompress  result : '" << buf << "'\n";

		{
			const char*	filename = "zip.raw";
			FILE*	fp = fopen( filename, "w" );
			fwrite( zbuf, zsize, 1, fp );
			fclose( fp );
		}

		delete [] zbuf;
		delete [] buf;
	}

	{
		char*	zbuf = NULL;
		size_t	zsize = 0;

		ZlibGzipCompressor	ZLIBGZIPCompressor;
		Compressor*	compressor_p = &ZLIBGZIPCompressor;
		fprintf( stdout, "compressor_p = %p\n", compressor_p );
		zbuf = compressor_p->compress( plains, strlen(plains), &zsize );
		if ( NULL == zbuf ) {
			std::cerr << "fail to compress in GZIP mode\n";
			return	1;
		}
		std::cout << "GZIP compress  size : " << strlen(plains) << " --> " << zsize << std::endl;

		char*	buf = NULL;
		size_t	size = 0;
		buf = compressor_p->decompress( zbuf, zsize, &size );
		if ( NULL == buf ) {
			std::cerr << "fail to decompress in GZIP mode\n";
			return	1;
		}
		std::cout << "GZIP decompress  size : " << zsize << " --> " << size << std::endl;
		std::cout << "GZIP decompress  result : '" << buf << "'\n";

		{
			const char*	filename = "zip.gzip";
			FILE*	fp = fopen( filename, "w" );
			fwrite( zbuf, zsize, 1, fp );
			fclose( fp );
		}

		delete [] zbuf;
		delete [] buf;
	}

	{
		char*	zbuf = NULL;
		size_t	zsize = 0;

		ZlibDeflateCompressor	ZLIBDEFLATECompressor;
		Compressor*	compressor_p = &ZLIBDEFLATECompressor;
		fprintf( stdout, "compressor_p = %p\n", compressor_p );
		zbuf = compressor_p->compress( plains, strlen(plains), &zsize );
		if ( NULL == zbuf ) {
			std::cerr << "fail to compress in DEFLATE mode\n";
			return	1;
		}
		std::cout << "DEFLATE compress  size : " << strlen(plains) << " --> " << zsize << std::endl;

		char*	buf = NULL;
		size_t	size = 0;
		buf = compressor_p->decompress( zbuf, zsize, &size );
		if ( NULL == buf ) {
			std::cerr << "fail to decompress in DEFLATE mode\n";
			return	1;
		}
		std::cout << "DEFLATE decompress  size : " << zsize << " --> " << size << std::endl;
		std::cout << "DEFLATE decompress  result : '" << buf << "'\n";

		{
			const char*	filename = "zip.deflate";
			FILE*	fp = fopen( filename, "w" );
			fwrite( zbuf, zsize, 1, fp );
			fclose( fp );
		}

		delete [] zbuf;
		delete [] buf;
	}

    return 0;
}


